package com.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * 描述：生成mapper注释
 * 作者：刘飞华
 * 日期：2024/7/24 17:37
 */
public class CustomMapperClassPlugin extends PluginAdapter {

    private final Properties properties;

    private String author;

    private String createTime;

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    public CustomMapperClassPlugin() {
        this.properties = new Properties();
    }

    public CustomMapperClassPlugin(Properties properties) {
        this.properties = properties;
    }


    @Override
    public void setProperties(Properties properties) {
        this.properties.putAll(properties);
        this.author = properties.getProperty("author");
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        this.createTime = dateFormatter.format(new Date());
    }

    /**
     * 生成 Mapper接口
     *
     * @param
     * @return
     * @author 刘飞华
     * @date: 2024/7/24 17:34
     */
    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {


        //设置注释
        String remarks = introspectedTable.getRemarks().replace("表", "");
        String tableNameAtRuntime = introspectedTable.getFullyQualifiedTableNameAtRuntime();
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine(" * 描述: " + remarks+"("+tableNameAtRuntime+")");
        interfaze.addJavaDocLine(" * 作者: " + author);
        interfaze.addJavaDocLine(" * 日期: " + createTime);
        interfaze.addJavaDocLine(" */");
        // 添加Mapper的注解
        interfaze.addAnnotation("@Mapper");


        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
        return super.clientGenerated(interfaze, introspectedTable);
    }


    /**
     * 生成entity
     * @param 
     * @return 
     * @author 刘飞华
     * @date: 2024/7/25 9:13
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addJavaDocLine("@Data");
        topLevelClass.addJavaDocLine("@Builder");
        topLevelClass.addJavaDocLine("@NoArgsConstructor");
        topLevelClass.addJavaDocLine("@AllArgsConstructor");

        topLevelClass.addSuperInterface(new FullyQualifiedJavaType("Serializable"));
        
        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.io.Serializable"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.*"));
        return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
    }

    /**
     * 生成setter方法
     *
     * @param
     * @return
     * @author 刘飞华
     * @date: 2024/7/24 17:44
     */
    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    /**
     * 生成getter方法
     *
     * @param
     * @return
     * @author 刘飞华
     * @date: 2024/7/24 17:44
     */
    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

}